<?php
/**
 * Object Relational Persistence Mapping Class
 * for table: assiged_test_item.
 * @package core.ordo
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class AssigedTestItem extends ORDataObject {

	/**#@+
	 * Fields of table: assiged_test_item mapped to class members
	 */
	var $assigned_test_item_id		= '';
	var $test_service_id		= '';
	var $test_diagnosis_ticket_id		= '';
	var $original_price     = '';
    var $insurance_price        = '';
    var $is_done        = '';
    
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'assiged_test_item';

	/**
	 * Primary Key
	 */
	var $_key = 'assigned_test_item_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'AssigedTestItem';

	/**
	 * Handle instantiation
	 */
	function __construct() {
		parent::ORDataObject();
	}
	
	public function addAssignedTestItem($data) {
		$this->populateArray($data);
		$this->persist();
		return $this->get_id();
	}
	public function removeAssignedTestItem($testDiagnosisTicketId) {
		$sql = "DELETE FROM $this->_table WHERE test_diagnosis_ticket_id ='" . $testDiagnosisTicketId . "'";
        $this->_execute($sql);
        
	}
	public function setDone($done) {
		$this->is_done = $done;
		$this->persist();
	}
	public function countTotalPrices4ATestTicket($testDiagnosisTicketId) {
		$prices = array('price'=>0, 'insurance_price'=>0, 'patient_price'=>0);
		$ds = new Datasource_sql();
        $sql = array(
            'cols'  => "SUM( original_price ) price, SUM( insurance_price ) insurance_price, ( SUM( original_price ) - SUM( insurance_price )) patient_price",
            'from'  => "$this->_table ",
            'where' => "test_diagnosis_ticket_id='$testDiagnosisTicketId' AND is_done='1'"
            );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        if (empty($result)) {
        	return $prices;
        }
        
        if (!isset($result[0]['price']) || $result[0]['price'] == null) {
        	$result[0]['price'] = 0;
        }
        
	    if (!isset($result[0]['insurance_price']) || $result[0]['insurance_price'] == null) {
            $result[0]['price'] = 0;
        }
        
	    if (!isset($result[0]['patient_price']) || $result[0]['patient_price'] == null) {
            $result[0]['patient_price'] = 0;            
        }
                
        return $result[0];
	}
	/**
	 *
	 * @param $testDiagnosisTicketId
	 * @param $assignedTest
	 * array([test_service_id],...)
	 * @return unknown_type
	 */

	public function addArrayOfTestItem($testDiagnosisTicketId, $assignedTest) {
		
		foreach ($assignedTest  as $testId) {
			$this->addAssignedTestItem($testDiagnosisTicketId, $testId);
		}
	}
	/**
	 * Get list of assigned test item of a given test ticket id.
	 * @param $testDiagnosisTicketId
	 * @return array(assigned_test_item_id, test_service_id)
	 */
	public function listOfAssignedTestItem($testDiagnosisTicketId, $toArray = false) {		
		$field    =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		
		$ds = new Datasource_sql();
		$sql = array(
            'cols'  => "TD.assigned_test_item_id ,TD.test_service_id, TD.original_price, 
                    TD.insurance_price,(TD.original_price - TD.insurance_price) as patient_price, 
                    S.$field name, test.test_id, test.$field test_name, TD.is_done",
            'from'  => "$this->_table TD INNER JOIN test_service TS 
            ON (TD.test_diagnosis_ticket_id = '" 
				. $testDiagnosisTicketId
				."' AND TD.test_service_id = TS.test_service_id) "
				." INNER JOIN service S ON TS.service_id = S.service_id " 
				." INNER JOIN test  ON test.test_id = TS.test_id ",               
            'where' => "test_diagnosis_ticket_id = '" . $testDiagnosisTicketId ."'"
            );
            $ds->setup($this->_db, $sql);
            $result = $ds->toArray();
            if (empty($result)) {
            	return null;
            }

            return $result;
	}
    /**
     * @return nothing.
     */
    public static function normalizeData4Display(&$data) {
        if (is_array($data)) {
            foreach ($data as &$item) {
            	$item['original_price'] = L10nHelper::renderNumber($item['original_price'], 0);
            	$item['insurance_price'] = L10nHelper::renderNumber($item['insurance_price'], 0);
            	$item['patient_price'] = L10nHelper::renderNumber($item['patient_price'], 0);
            }
        }
    }


}
?>
